let mBannerAd = null,
    mVideoAd = null,
    wxVideoId = null,
    wxBannerId = null;


var WXAds = cc.Class({


    statics: {

        init:function(wxBannerId,wxVideoId){
          this.wxBannerId = wxBannerId;
          this.wxVideoId = wxVideoId;
            mVideoAd = wx.createRewardedVideoAd({
                adUnitId: this.wxVideoId
            })
        },


        /**
         * 播放banner
         */
        showBanner:function(){
            let screenSize = wx.getSystemInfoSync();
            if (screenSize.SDKVersion < '2.0.4') { //不满足显示广告的SDK版本
                return;
            }
            if (mBannerAd) {
                mBannerAd.destory();
            }

            var showWidth = screenSize.screenWidth;
            mBannerAd = wx.createBannerAd({
                adUnitId: GAME_INFO.BANNER_ID,
                style: {
                    left: 0,
                    top: 0,
                    width: showWidth,
                }
            });

            mBannerAd.onResize(res => {
                let left = (screenSize.screenWidth - res.width) / 2;
                let top = screenSize.screenHeight - res.height;

                // if (UserData.getIsIPhoneX()) {
                //     top -= 20;
                // }
                mBannerAd.style.top = top;
                mBannerAd.style.left = left;
                mBannerAd.show();
            });

        },

        showVide:function(callback){
            if (!mVideoAd) {
                mVideoAd = wx.createRewardedVideoAd({
                    adUnitId: this.wxVideoId
                })
            }
            mVideoAd.load()
                .then(() => mVideoAd.show())
                .catch(err => Log.error(err.errMsg))

            var thisCallback = function (res) {
                if (callback) {
                    if (res && res.isEnded || res === undefined) {
                        // 正常播放结束，可以下发游戏奖励
                        callback(true);
                    }
                    else {
                        // 播放中途退出，不下发游戏奖励
                        callback(false);
                    }
                }
                SystemInfo.setIsPlayingVideo(false)
                SoundUtil.resumeMusic();
                mVideoAd.offClose(thisCallback);
            };

            mVideoAd.onClose(thisCallback);
            return;
        }

    }
});

module.exports = WXAds;